Imports System
Imports System.Data
Imports System.Xml

Namespace Gigiti
    Public Class Common
        Public Shared FooterTemplate As String
        Public Shared FooterTemplateStart As String
        Public Shared FooterTemplateEnd As String
        Public Shared YahooWebsearchTemplate As String
        Public Shared YahooWebsearchTopTenTemplate As String
        Public Shared EbayWebsearchTemplate As String
        Public Shared TopTenRankTemplate As String
        Public Shared ViewCommentsTemplate As String
        Public Shared YahooImageSearchTemplate As String
        Public Shared YahooVideoSearchTemplate As String
        Public Shared YahooNewsSearchTemplate As String
        Public Shared YahooRecentNewsTemplate As String

        Public Shared CacheRootFilePath As String
        Public Shared DataExtension As String
        Public Shared InfoExtension As String
       


        Public Shared Function ResolveUrl(ByVal originalUrl As String) As String
            If originalUrl Is Nothing Then
                Return Nothing
            End If
            ' *** Absolute path - just return
            If originalUrl.IndexOf("://") <> -1 Then
                Return originalUrl
            End If
            ' *** Fix up image path for ~ root app dir directory
            If originalUrl.StartsWith("~") Then
                Dim newUrl As String = ""
                If HttpContext.Current IsNot Nothing Then
                    newUrl = HttpContext.Current.Request.ApplicationPath + originalUrl.Substring(1).Replace("//", "/")
                Else
                    Throw New ArgumentException("Invalid URL: Relative URL not allowed.")
                    ' *** Not context: assume current directory is the base directory
                End If
                ' *** Just to be sure fix up any double slashes
                Return newUrl
            End If
            Return originalUrl
        End Function

        Public Shared Function ResolveServerUrl(ByVal serverUrl As String, ByVal forceHttps As Boolean) As String
            ' *** Is it already an absolute Url?
            If serverUrl.IndexOf("://") > -1 Then
                Return serverUrl
            End If
            ' *** Start by fixing up the Url an Application relative Url
            Dim newUrl As String = ResolveUrl(serverUrl)
            Dim originalUri As Uri = HttpContext.Current.Request.Url
            newUrl = (IIf(forceHttps, "https", originalUri.Scheme)) + "://" + originalUri.Authority + newUrl
            Return newUrl
        End Function

        Public Shared Sub Initialize()
            Dim oXmlDocument As New XmlDocument()
            oXmlDocument.Load(System.Web.HttpContext.Current.Server.MapPath("~/XML/JSONTemplate.xml"))
            FooterTemplate = oXmlDocument.LastChild.Item("FooterTemplate").InnerText
            FooterTemplateStart = oXmlDocument.LastChild.Item("FooterTemplateStart").InnerText
            FooterTemplateEnd = oXmlDocument.LastChild.Item("FooterTemplateEnd").InnerText
            YahooWebsearchTemplate = oXmlDocument.LastChild.Item("YahooWebsearchTemplate").InnerText
            YahooWebsearchTopTenTemplate = oXmlDocument.LastChild.Item("YahooWebsearchTopTenTemplate").InnerText
            EbayWebsearchTemplate = oXmlDocument.LastChild.Item("EbayWebsearchTemplate").InnerText
            TopTenRankTemplate = oXmlDocument.LastChild.Item("TopTenRankTemplate").InnerText
            ViewCommentsTemplate = oXmlDocument.LastChild.Item("ViewCommentsTemplate").InnerText
            YahooImageSearchTemplate = oXmlDocument.LastChild.Item("YahooImageSearchTemplate").InnerText
            YahooVideoSearchTemplate = oXmlDocument.LastChild.Item("YahooVideoSearchTemplate").InnerText
            YahooNewsSearchTemplate = oXmlDocument.LastChild.Item("YahooNewsSearchTemplate").InnerText
            YahooRecentNewsTemplate = oXmlDocument.LastChild.Item("YahooRecentNewsTemplate").InnerText

            ' root folder for cached data files
            CacheRootFilePath = System.Web.HttpContext.Current.Server.MapPath("~/Cache")

            ' file extensions for the cached object and cache information files
            DataExtension = ".cachedata"
            InfoExtension = ".cacheinfo"

        End Sub
    End Class
End Namespace

